package com.yonyou.brigade.openapi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.basic_init.service.IMongoService;
import com.yonyou.brigade.basic_init.service.IUserCenterService;
import com.yonyou.brigade.openapi.service.IApplySyncService;
import com.yonyou.brigade.service.IBrigadePublicService;
import com.yonyou.brigade.util.operateutil.JsonUtils;
import com.yonyou.ybzboot.starter.mongodb.dao.IMongoDao;
import com.yonyou.ybzboot.starter.mongodb.model.CriteriaMap;
import com.yonyou.ybzboot.uc.dto.UcUserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class ApplySyncServiceImpl implements IApplySyncService {

    @Autowired
    private IBrigadePublicService brigadePublicService;

    @Autowired
    private IUserCenterService userCenterService;

    @Autowired
    private IMongoDao mongoDao;

    @Autowired
    private IMongoService mongoService;

    private final String APPLY_INFO_OPENAPI = "apply_info_openapi";

    private final String PARAMORIGINAL = "paramOriginal";

    private final String APPLY_INFO_RESULT = "apply_info_result";

    private final String APPLYINFORESULT = "applyInfoResult";

    @Override
    public JSONObject applySync(String phone, String tenantId, JSONObject applyInfo) {
        // 保存原始入参到mongo
        saveParamToMongo(phone, tenantId, applyInfo);
        JSONObject result = new JSONObject();
        result.put("isSync", Boolean.FALSE);
        UcUserDto ucUserDto = userCenterService.getCompleteUcUserDtoByPhone(phone, tenantId);
        String orgPk = ucUserDto != null ? ucUserDto.getOrgPk() : "";
        String resultStr = brigadePublicService.pushTravelApplyOrder(tenantId, orgPk, applyInfo.getString("channel"), applyInfo);
        if (!StringUtils.isEmpty(resultStr) && JsonUtils.isJSONObject(resultStr)) {
            JSONObject resultJSON = JSONObject.parseObject(resultStr);
            if (resultJSON.getBoolean("success")) {
                result.put("isSync", Boolean.TRUE);
            } else {
                result.put("msg", CollectionUtils.isEmpty(resultJSON.getJSONArray("msgArr")) ? null : resultJSON.getJSONArray("msgArr").get(resultJSON.getJSONArray("msgArr").size()-1));
            }
            if (!StringUtils.isEmpty(applyInfo.getString("channel"))) {
                mongoService.saveApplyResultToMongo(tenantId, applyInfo.getString("channel"), applyInfo, result);
            }
        }
        return result;
    }

    @Override
    public Map queryApplyInfo(String phone, String tenantId, JSONObject param) {
        List<HashMap> mapList = new ArrayList<>();
        CriteriaMap criteriaMap = new CriteriaMap();
        criteriaMap.put("tenantId", tenantId);
        criteriaMap.put("key", param.getString("key"));
        criteriaMap.put("billNum", param.getString("billNum"));
        switch (param.getString("key")) {
            case PARAMORIGINAL:
                mapList = mongoDao.findByMap(criteriaMap, HashMap.class, APPLY_INFO_OPENAPI);
                break;
            case APPLYINFORESULT:
                mapList = mongoDao.findByMap(criteriaMap, HashMap.class, APPLY_INFO_RESULT);
                break;
            default:
                break;
        }
        if (CollectionUtils.isEmpty(mapList)) {
            return null;
        }
        return mapList.get(0);
    }

    private void saveParamToMongo(String phone, String tenantId, JSONObject applyInfo) {
        CriteriaMap criteriaMap = new CriteriaMap();
        criteriaMap.put("tenantId", tenantId);
        criteriaMap.put("key", PARAMORIGINAL);
        criteriaMap.put("billNum", applyInfo.getString("billNum"));
        List<HashMap> mapList = mongoDao.findByMap(criteriaMap, HashMap.class, APPLY_INFO_OPENAPI);
        if (!CollectionUtils.isEmpty(mapList)) {
            mongoDao.deleteByMap(criteriaMap, APPLY_INFO_OPENAPI);
        }
        JSONObject applyInfoMongo = new JSONObject();
        applyInfoMongo.put("key", PARAMORIGINAL);
        applyInfoMongo.put("applyInfo", applyInfo);
        applyInfoMongo.put("tenantId", tenantId);
        applyInfoMongo.put("phone", phone);
        applyInfoMongo.put("billNum", applyInfo.getString("billNum"));
        applyInfoMongo.put("channel", applyInfo.getString("channel"));
        applyInfoMongo.put("pushTime", new Date());
        mongoDao.getMongoTemplate().save(applyInfoMongo, APPLY_INFO_OPENAPI);
    }
}
